package com.wteam.controller;

import com.wteam.PoJo.DTO.buygood;
import com.wteam.PoJo.DTO.listorder;
import com.wteam.PoJo.VO.buyorder;
import com.wteam.PoJo.VO.changegoods;
import com.wteam.PoJo.VO.orderid;
import com.wteam.PoJo.VO.ordervo;
import com.wteam.service.GoodsCartServicer;
import com.wteam.service.OrderService;
import com.wteam.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "订单模块")
@RequestMapping("/order")

public class OrderController {
    @Autowired
    private GoodsCartServicer goodsCartServicer;

    @Autowired
    private OrderService orderService;

    /**
     *
     * @return
     */
    @Operation(summary = "点击结算,订单预览")
    @GetMapping("/buyorder")
    public Result<ordervo> buycart() {
        return goodsCartServicer.buycart();

    }

    /**
     *
     * @param changegoods
     * @return
     */
    @Operation(summary = "勾选商品(注意每次用户改变数量和勾选时都要)")
    @PostMapping("/choosegoods")
    @CacheEvict(value = "goods_cart", allEntries = true)
    public Result<Integer> choosecart(@RequestBody @Validated List<changegoods> changegoods) {
        return goodsCartServicer.sumcart(changegoods);
    }

    /**
     *
     * @param buyorder
     * @return
     */
    @Operation(summary = "购物车下单(可支付也可不支付)")
    @PostMapping("/saveorder")
    @CacheEvict(value = "goods_cart", allEntries = true)
    public Result<orderid> saveorder(@RequestBody buyorder buyorder) {
        return orderService.buyorder(buyorder);
    }

    /**
     *
     * @param id
     * @return
     */
    @Operation(summary = "取消订单")
    @PostMapping("/deleteorder/{id}")
    public Result<String> deleteorder(@PathVariable @ApiParam("传id") Integer id) {
        return orderService.deleteorder(id);
    }

    /**
     *
     * @param orderStatus
     * @return
     */
    @Operation(summary = "查看订单")
    @GetMapping("/selectorder/{orderStatus}")
    public Result<List<listorder>> listorder(@PathVariable @ApiParam("订单状态1.未付款2.已付款 3.已取消 4.全部(不包括取消的) 5：取消订单的商品") Integer orderStatus) {
        return orderService.selectorder(orderStatus);
    }

    /**
     *
     * @param orderId
     * @param ordersnId
     * @return
     */
    @Operation(summary = "进行支付")
    @GetMapping("/order/{orderId}/{ordersnId}")
    public Result putmoney(@ApiParam("订单id") @PathVariable Integer orderId
            , @ApiParam("订单编号") @PathVariable /*@JsonSerialize(using = ToStringSerializer.class)*/ Long ordersnId) {
        return orderService.putmoney(orderId, ordersnId);
    }

    /**
     *
     * @param buygood
     * @return
     */
    @Operation(summary = "单个商品下单(可支付也可不支付)")
    @PostMapping("/saveonegoods")
    public Result<orderid> saveonegoods(@RequestBody buygood buygood) {
        return orderService.saveonegoods(buygood);
    }

    /**
     *
     * @param orderid
     * @return
     */
    @Operation(summary = "根据订单id查订单详情")
    @GetMapping("/orderdetial/{orderid}")
    public Result<listorder> orderdetial(@PathVariable @ApiParam("订单id") Integer orderid) {
        return orderService.orderdetial(orderid);
    }


}
